package com.example.jbmsuppliers.sinopec;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.support.v4.content.FileProvider;

import java.io.File;

public class UpdateVersionRecevie extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        if(Build.VERSION.SDK_INT>=24) {//判读版本是否在7.0以上
            String file =Environment.getExternalStorageDirectory()+"/download/JBM.apk";
//            Toast.makeText(context,file.getAbsolutePath()+"  ",Toast.LENGTH_LONG).show();
            Uri apkUri = FileProvider.getUriForFile(context, "com.example.jbmsuppliers.fileprovider", new File(file));//在AndroidManifest中的android:authorities值
            Intent install = new Intent(Intent.ACTION_VIEW);
            install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//添加这一句表示对目标应用临时授权该Uri所代表的文件
            install.setDataAndType(apkUri, "application/vnd.android.package-archive");
            context.startActivity(install);
        } else{
            Intent install = new Intent(Intent.ACTION_VIEW);
            String fileName=Environment.getExternalStorageDirectory()+"/download/JBM.apk";
            install.setDataAndType(Uri.fromFile(new File(fileName)), "application/vnd.android.package-archive");
            install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(install);
        }

//        intent = new Intent(Intent.ACTION_VIEW);
//        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//        File file = new FileStorage().updateFile();
//        Uri imageUri = FileProvider.getUriForFile(context, "com.bugull.cameratakedemo.fileprovider", file);//通过FileProvider创建一个content类型的Uri
//        intent.setDataAndType(imageUri,
//                "application/vnd.android.package-archive");
//        context.startActivity(intent);
    }
}
